package com.web.app.util;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

import java.util.Collections;

/**
 * @author JMLF
 * @date 2022-09-21
 */
public class AutoCodeUtil {
    public static void main(String[] args) {
        //1.配置DataSourceConfig信息
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(
                "jdbc:mysql://localhost:3306/pkuku?serverTimezone=UTC&characterEncoding=utf8",
                "root",
                "123456")
                .build();

        //2.配置全局信息
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .fileOverride() //覆盖原有文件
                .outputDir("D:\\EV\\IDEA\\WorkSpace\\rj2003\\src\\main\\java") //指定文件生成后的目录
                .author("林") // 指定作者
                .enableSwagger() // 开启swagger
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
                .build();

        //3.配置包的信息
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.web")
                .moduleName("app")
                .entity("pojo")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("dao")
                .xml("mapper.xml")
                .controller("ctrl")
                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\EV\\IDEA\\WorkSpace\\rj2003\\src\\main\\resources\\mapper"))
                .build();

        //4.准备执行的文件,集合所有配置信息
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig);
        autoGenerator.global(globalConfig);
        autoGenerator.packageInfo(packageConfig);

        //5.开始执行代码信息
        autoGenerator.execute();
    }
}
